package com.game.centerserver.utils;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.alibaba.fastjson.JSONPath;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

public class HttpServletUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpServletUtil.class);
    private static final String LOCAL_IP = "127.0.0.1";
    private static final String AGENT_SOURCE_IP = "Agent-Source-Ip";
    private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
    private static final String USER_AGENT_HTTP_HEADER = "User-Agent";

    public HttpServletUtil() {
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw new RuntimeException("HTTP_CONTEXT_ERROR");
        } else {
            return requestAttributes.getRequest();
        }
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw new RuntimeException("HTTP_CONTEXT_ERROR");
        } else {
            return requestAttributes.getResponse();
        }
    }

    public static String getRequestClientIp(HttpServletRequest request) {
        if (ObjectUtil.isEmpty(request)) {
            return "127.0.0.1";
        } else {
            //String remoteHost = ServletUtil.getClientIP(request, new String[]{"Agent-Source-Ip"});
            //return "0:0:0:0:0:0:0:1".equals(remoteHost) ? "127.0.0.1" : remoteHost;
            return "127.0.0.1";
        }
    }

    public static String calcClientIpAddress(HttpServletRequest request, String ipGeoApi, String ipGeoAppCode) {
        String resultJson = "-";
        String requestApiHeader = "Authorization";
        String ip = getRequestClientIp(request);
        if (!ObjectUtil.isEmpty(ip) && !NetUtil.isInnerIP(ip)) {
            if (ObjectUtil.hasEmpty(new Object[]{ipGeoApi, ipGeoAppCode})) {
                return resultJson;
            } else {
                try {
                    if (ObjectUtil.isAllNotEmpty(new Object[]{ipGeoApi, ipGeoAppCode})) {
                        String jsonPath = "$['data']['country','region','city','isp']";
                        String appCodeSymbol = "APPCODE";
                        HttpRequest http = HttpUtil.createGet(String.format(ipGeoApi, ip));
                        http.header(requestApiHeader, appCodeSymbol + " " + ipGeoAppCode);
                        resultJson = http.timeout(3000).execute().body();
                        resultJson = String.join("", (List) JSONPath.read(resultJson, jsonPath));
                    }
                } catch (Exception var9) {
                    log.error("根据ip定位异常，具体信息为：{}", var9.getMessage());
                }

                return resultJson;
            }
        } else {
            return resultJson;
        }
    }

    public static UserAgent getUserAgent(HttpServletRequest request) {
        //String userAgentStr = ServletUtil.getHeaderIgnoreCase(request, "User-Agent");
        //return ObjectUtil.isNotEmpty(userAgentStr) ? UserAgentUtil.parse(userAgentStr) : null;
        return null;
    }

    public static Boolean getNormalRequestFlag(HttpServletRequest request) {
        return request.getHeader("Accept") == null || request.getHeader("Accept").toLowerCase().contains("text/html");
    }

    public static Boolean getJsonRequestFlag(HttpServletRequest request) {
        return request.getHeader("Accept") == null || request.getHeader("Accept").toLowerCase().contains("application/json");
    }
}
